package com.cheyun.netsalev3.view.search

import android.content.Intent
import android.view.inputmethod.EditorInfo
import android.widget.RelativeLayout
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.bean.CarBrandParam
import com.cheyun.netsalev3.bean.DialogParam
import com.cheyun.netsalev3.databinding.ActivityFilterBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.FilterActivityViewModel
import com.google.android.material.tabs.TabLayout

class FilterActivity : BaseActivity<ActivityFilterBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_filter
    }

    private lateinit var filterType1LeftAdapter: MyAdapter
    private lateinit var filterType1RightAdapter: MyAdapter

    private lateinit var filterType2LeftAdapter: MyAdapter

    private lateinit var filterType3MemberAdapter: MyAdapter
    private lateinit var filterType3MemberGroupAdapter: MyAdapter

    private lateinit var filterType4SortAdapter: MyAdapter

    override fun initViewModel(): ViewModel? {
        var factory = InjectorUtils.getFilterActivityViewModelFactory()
        return ViewModelProviders.of(this, factory).get(FilterActivityViewModel::class.java)
    }

    var type = 1

    //分类选中的值
    var type1HashMap = HashMap<String, String>()
    var type1HashMapDesc = ArrayList<DialogParam>()
    //时间选中的值
    var type2HashMap = HashMap<String, String>()
    //用户选中的值
    var type3HashMap = HashMap<String, String>()
    //排序选中的值
    var type4HashMap = HashMap<String, String>()

    var top = 0

    override fun initView() {
        if (intent.hasExtra("type")) {
            type = intent.getIntExtra("type", 1)
        }
        if (intent.hasExtra("type1Data")) {
            type1HashMap = intent.getSerializableExtra("type1Data") as HashMap<String, String>
        }
        if (intent.hasExtra("type1DataDesc")) {
            type1HashMapDesc = intent.getSerializableExtra("type1DataDesc") as ArrayList<DialogParam>
        }
        if (intent.hasExtra("type2Data")) {
            type2HashMap = intent.getSerializableExtra("type2Data") as HashMap<String, String>
        }
        if (intent.hasExtra("type3Data")) {
            type3HashMap = intent.getSerializableExtra("type3Data") as HashMap<String, String>
        }
        if (intent.hasExtra("type4Data")) {
            type4HashMap = intent.getSerializableExtra("type4Data") as HashMap<String, String>
        }
        if(intent.hasExtra("top")){
            top = intent.getIntExtra("top",0)
        }
        var params = window.attributes
        params.y = top
        window.attributes = params
        window.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)

//        val childHeight: Int = binding.topView.getMeasuredHeight()
//        val childWidth: Int = binding.topView.getMeasuredWidth()
//        binding.topView?.layout(0,top,childWidth,top+childHeight)
//        binding.topView.top = top

        filterType1LeftAdapter = MyAdapter(R.layout.filter_type1_left_item, binding.viewModel)
        binding.filterType1Left.adapter = filterType1LeftAdapter

        filterType1RightAdapter = MyAdapter(R.layout.filter_type1_right_item, binding.viewModel)
        binding.filterType1RightList.adapter = filterType1RightAdapter

        filterType2LeftAdapter = MyAdapter(R.layout.filter_type2_left_item, binding.viewModel)
        binding.filterType2Left.adapter = filterType2LeftAdapter

        filterType3MemberAdapter = MyAdapter(R.layout.filter_type3_member_item, binding.viewModel)
        binding.filterType3Member.adapter = filterType3MemberAdapter

        filterType3MemberGroupAdapter = MyAdapter(R.layout.filter_type3_member_group_item, binding.viewModel)
        binding.filterType3MemberGroup.adapter = filterType3MemberGroupAdapter

//        filterType4SortAdapter = MyAdapter(R.layout.filter_type4_sort_item, binding.viewModel)
//        binding.filterType4Sort.adapter = filterType4SortAdapter

        binding.tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                binding.viewModel?.onChangeFilter3Type(tab.position + 1)
            }

            override fun onTabUnselected(tab: TabLayout.Tab) {

            }

            override fun onTabReselected(tab: TabLayout.Tab) {

            }
        })
        binding.searchMember.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
                binding.viewModel?.searchMember(v, v.text.toString())
                true
            } else
                false
        }
    }

    override fun initData() {
        binding.viewModel?.filterType?.postValue(type)
        binding.viewModel?.setInitFilter(type1HashMap, type1HashMapDesc, type2HashMap, type3HashMap, type4HashMap)

        binding.viewModel?.filterType1LeftDate?.observe(this, Observer { lists ->
            filterType1LeftAdapter.submitList(lists)
        })

        binding.viewModel?.filterType1RgihtList?.observe(this, Observer { lists ->
            filterType1RightAdapter.submitList(lists)
        })

        binding.viewModel?.filterType2LeftDate?.observe(this, Observer { lists ->
            filterType2LeftAdapter.submitList(lists)
        })

        binding.viewModel?.filterType3MemberDate?.observe(this, Observer { lists ->
            filterType3MemberAdapter.submitList(lists)
        })

        binding.viewModel?.filterType3MemberGroupDate?.observe(this, Observer { lists ->
            filterType3MemberGroupAdapter.submitList(lists)
        })

//        binding.viewModel?.filterType4Date?.observe(this, Observer { lists ->
//            filterType4SortAdapter.submitList(lists)
//        })

        binding.viewModel?.type1HashMapDescLivData?.observe(this, Observer { lists ->
            binding.cancelButton.text = "已选择" + lists.size + "项"
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 102) {
            if (requestCode == 101 || requestCode == 103 || requestCode == 104) {
                data?.apply {
                    var carBrandParam = getSerializableExtra("car") as CarBrandParam
                    binding.viewModel?.setCarData(carBrandParam)
                }
            }
            if (requestCode == 102) {
                data?.apply {
                    var carBrandParam = getSerializableExtra("car") as CarBrandParam
                    binding.viewModel?.setCarData2(carBrandParam)
                }
            }
        }
    }


}
